package com.hdax.dmw.scheduler.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dmw.dtos.scheduler.SchedulerSeatDto;
import com.hdax.dmw.dtos.scheduler.SeatArrayDto;
import com.hdax.dmw.entity.scheduler.DmSchedulerSeat;
import com.hdax.dmw.entity.scheduler.DmSchedulerSeatPrice;
import com.hdax.dmw.scheduler.mapstruct.SeatInfoMapper;
import com.hdax.dmw.scheduler.mapstruct.SeatPriceMapper;
import com.hdax.dmw.scheduler.service.DmSchedulerSeatPriceService;
import com.hdax.dmw.scheduler.service.DmSchedulerSeatService;
import com.hdax.dmw.scheduler.mappers.DmSchedulerSeatMapper;
import com.hdax.dmw.utils.Result;
import com.hdax.dmw.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author guoyisha
* @description 针对表【dm_scheduler_seat(排期座位表)】的数据库操作Service实现
* @createDate 2023-06-15 12:16:43
*/
@Service
public class DmSchedulerSeatServiceImpl extends ServiceImpl<DmSchedulerSeatMapper, DmSchedulerSeat>
    implements DmSchedulerSeatService{

    @Override
    public Integer getSaleCount(Long scheduleId) {
        QueryWrapper<DmSchedulerSeat> queryWrapper
                = new QueryWrapper<>();
        queryWrapper.eq("scheduleId",scheduleId);
        queryWrapper.isNotNull("orderNo");
        queryWrapper.isNotNull("userId");
        queryWrapper.in("status",2,3);
        return baseMapper.selectCount(queryWrapper).intValue();
    }

    @Autowired
    private SeatPriceMapper seatPriceMapper;
    @Autowired
    private SeatInfoMapper seatInfoMapper;
    @Autowired
    private DmSchedulerSeatPriceService priceService;

    @Override
    public Result<SchedulerSeatDto> getSchedule(Long scheduleId) {
        SchedulerSeatDto dto = new SchedulerSeatDto();
        dto.setScheduleId(scheduleId);
        //从数据库查询DmSchedulerSeatPrice集合
        List<DmSchedulerSeatPrice> priceList = priceService.list(new QueryWrapper<DmSchedulerSeatPrice>().eq("scheduleId", scheduleId));
        List<SchedulerSeatDto.SeatPrice> prices = seatPriceMapper.toTargetList(priceList);
        dto.setSeatPriceList(prices);

        List<DmSchedulerSeat> seatInfos = baseMapper.getSeatInfos(scheduleId);

        List<SchedulerSeatDto.SeatInfo> seats = seatInfoMapper.toTargetList(seatInfos);
        dto.setSeatInfoList(seats);
        return ResultGenerator.genSuccessResult(dto);
    }

    @Override
    public Result<SeatArrayDto> getSeatList(Long scheduleId) {
        List<DmSchedulerSeat> seats = baseMapper.selectList(new QueryWrapper<DmSchedulerSeat>().eq("scheduleId", scheduleId));
        Map<Integer, List<DmSchedulerSeat>> map = seats.stream().collect(Collectors.groupingBy(DmSchedulerSeat::getX));
        List<String> seatsString = new ArrayList<>();
        map.keySet().forEach(key->{
            List<DmSchedulerSeat> dmSchedulerSeats = map.get(key);
            StringBuffer buffer = new StringBuffer();
            dmSchedulerSeats.forEach(seat->{
                if(seat.getAreaLevel() == 1){
                    buffer.append("a");
                }else if(seat.getAreaLevel() == 2){
                    buffer.append("b");
                }else if(seat.getAreaLevel() == 3){
                    buffer.append("c");
                }else{
                    buffer.append("_");
                }
            });
            seatsString.add(buffer.toString());
        });
        SeatArrayDto dto = new SeatArrayDto();
        dto.setSeatArray(seatsString);
        return ResultGenerator.genSuccessResult(dto);
    }

    @Override
    public DmSchedulerSeat detail(Long scheduleId, Integer x, Integer y) {
        return baseMapper.detail(scheduleId, x, y);
    }
}




